package com.cyt.website.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.website.dao.WebsiteAdvertisementDao;
import com.cyt.website.model.po.WebsiteAdvertisement;
import com.cyt.website.model.vo.WebsiteAdvertisementVo;
import com.cyt.website.service.WebsiteAdvertisementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网广告管理-官网广告管理Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-20 8:28:25
 */
@Service("websiteAdvertisementService")
public class WebsiteAdvertisementServiceImpl extends BaseServiceImpl<WebsiteAdvertisement> implements WebsiteAdvertisementService {

    @Resource
    private SysFileService sysFileService;
    @Resource
    private WebsiteAdvertisementDao websiteAdvertisementDao;

    @Autowired
    public void setBaseDao(WebsiteAdvertisementDao websiteAdvertisementDao) {
        this.baseDao = websiteAdvertisementDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteAdvertisementVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-20 8:28:25
     */
    @Override
    public List<WebsiteAdvertisement> listExample(WebsiteAdvertisementVo websiteAdvertisementVo) {
        Example example = new Example(WebsiteAdvertisement.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(websiteAdvertisementVo.getTitle())) {
            criteria.andLike("title", "%" + websiteAdvertisementVo.getTitle() + "%");
        }
        if (StringUtils.isNotEmpty(websiteAdvertisementVo.getStatus())) {
            criteria.andEqualTo("status", websiteAdvertisementVo.getStatus());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return websiteAdvertisementDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteAdvertisementVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-20 8:28:25
     */
    @Override
    public ReturnResult page(WebsiteAdvertisementVo websiteAdvertisementVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteAdvertisement> list = listExample(websiteAdvertisementVo);
        PageInfo<WebsiteAdvertisement> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteAdvertisementVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-20 8:28:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteAdvertisementVo websiteAdvertisementVo, SysUser sysUser) {
        if (StringUtils.isBlank(websiteAdvertisementVo.getId())) {
            WebsiteAdvertisement websiteAdvertisement = new WebsiteAdvertisement(sysUser);
            websiteAdvertisement.setTitle(websiteAdvertisementVo.getTitle());
            websiteAdvertisement.setPhoto(websiteAdvertisementVo.getPhoto());
            websiteAdvertisement.setPath(getFilePath(websiteAdvertisementVo.getPhoto()));
            websiteAdvertisement.setUrl(websiteAdvertisementVo.getUrl());
            websiteAdvertisement.setType(websiteAdvertisementVo.getType());
            websiteAdvertisement.setContent(websiteAdvertisementVo.getContent());
            websiteAdvertisement.setDescription(websiteAdvertisementVo.getDescription());
            websiteAdvertisement.setStatus(websiteAdvertisementVo.getStatus());
            websiteAdvertisement.setOrderNum(Integer.parseInt(websiteAdvertisementVo.getOrderNum()));
            websiteAdvertisement.setSeoKeywords(websiteAdvertisementVo.getSeoKeywords());
            websiteAdvertisement.setSeoDescription(websiteAdvertisementVo.getSeoDescription());
            websiteAdvertisementDao.insert(websiteAdvertisement);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WebsiteAdvertisement websiteAdvertisement = websiteAdvertisementDao.selectByPrimaryKey(websiteAdvertisementVo.getId());
            websiteAdvertisement.setTitle(websiteAdvertisementVo.getTitle());
            websiteAdvertisement.setPhoto(websiteAdvertisementVo.getPhoto());
            websiteAdvertisement.setPath(getFilePath(websiteAdvertisementVo.getPhoto()));
            websiteAdvertisement.setUrl(websiteAdvertisementVo.getUrl());
            websiteAdvertisement.setType(websiteAdvertisementVo.getType());
            websiteAdvertisement.setContent(websiteAdvertisementVo.getContent());
            websiteAdvertisement.setDescription(websiteAdvertisementVo.getDescription());
            websiteAdvertisement.setStatus(websiteAdvertisementVo.getStatus());
            websiteAdvertisement.setOrderNum(Integer.parseInt(websiteAdvertisementVo.getOrderNum()));
            websiteAdvertisement.setSeoKeywords(websiteAdvertisementVo.getSeoKeywords());
            websiteAdvertisement.setSeoDescription(websiteAdvertisementVo.getSeoDescription());
            websiteAdvertisement.setUpdateTime(new Date());
            websiteAdvertisement.setUpdateUser(sysUser.getId());
            websiteAdvertisementDao.updateByPrimaryKey(websiteAdvertisement);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteAdvertisementVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-20 8:28:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteAdvertisementVo websiteAdvertisementVo, SysUser sysUser) {
        //要修改的字段
        WebsiteAdvertisement websiteAdvertisement = new WebsiteAdvertisement();
        websiteAdvertisement.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        websiteAdvertisement.setUpdateUser(sysUser.getId());
        websiteAdvertisement.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteAdvertisement.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteAdvertisementVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteAdvertisementVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        websiteAdvertisementDao.updateByExampleSelective(websiteAdvertisement, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteAdvertisementVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-20 8:28:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteAdvertisementVo websiteAdvertisementVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteAdvertisement.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteAdvertisementVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteAdvertisementVo.getId().split(",")));
        }
        websiteAdvertisementDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }
}